<?php
session_start();
    
    // Created By : Julian Ponce
    // B.S. Computer Science - De La Salle Canlubang
    // November 5, 2011
    // IN PARAMETERS : attachmentId
    // 
    // This AJAX Request checks if the attachmentId belongs to the logged user and
    // it has no main document. This ajax is used for merging an attachment to a main document.
    // The requesting php script for this ajax is : viewPageSending.php

    include('../includes/siteConfig.php');
    
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

    // GET VARIABLES VIA POST METHOD
    $attachmentId = $_POST['attachmentId'];
    
    // GET LOGGED USER ID IN SESSION
    $loggedUserId = $_SESSION['loggedUserId'];
     
    // VERIFY ATTACHMENT ID
    $query = 
        "
            SELECT *
            FROM Attachment
            WHERE Attachment.attachmentId = '$attachmentId' AND 
                  Attachment.location = '$loggedUserId' AND
                  Attachment.mainDocumentId is NULL AND
                  Attachment.status = 'Active'
        ";
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
         
    // IF THE DOCUMENT CAN BE MERGED, RETURN 1
    if (mysql_num_rows($result) > 0) {
        echo json_encode (1);
    }
    
    // IF THE DOCUMENT CANNOT BE MERGED, RETURN 0
    else{
        echo json_encode (0);
    }

    
    mysql_free_result($result);
    mysql_close($connect);
?>
